package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 同步课程--详情页--推荐课程
 * 
 * 推荐同贡献者的2个课程，如没有同贡献者的就推荐同分类下的2个课程
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncParticularRecommendSrhController
        extends YxyApiBaseController {

    /** 目录 */
    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 课程 */
    @Autowired
    private YxyLessonsSynchroLessonRepository yxyLessonsSynchroLessonRepository;

    /** 学科 */
    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    /** 版本 */
    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    /** 课时 */
    @Autowired
    private YxyLessonsSynchroLessonItemRepository yxyLessonsSynchroLessonItemRepository;

    /** 视频 */
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/particular/recommend/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        List<RespModel> retuLst = new ArrayList<>();

        // 同贡献者的2个课程
        YxyLessonsSynchroLesson yxyLessonsSynchroLessons = yxyLessonsSynchroLessonRepository
                .findById(lessonId);
                // String aaName = yxyLessonsSynchroLessons.getAuthor();
                // List<YxyLessonsSynchroLesson> yxyLessonsSynchroLessonsLst =
                // yxyLessonsSynchroLessonRepository
                // .findTop2ByAuthorAndStatusAndIsVisibleOrderByVvDesc(aaName,
                // 2,
                // 1);
                // if (yxyLessonsSynchroLessonsLst.size() >= 2) {
                // for (YxyLessonsSynchroLesson yxyLessonsSynchroLesson :
                // yxyLessonsSynchroLessonsLst) {
                // RespModel model = new RespModel();
                // model.vip = yxyLessonsSynchroLesson.getVip();
                // model.cover = yxyLessonsSynchroLesson.getCover();
                // model.count = yxyLessonsSynchroLesson.getCount();
                // model.duration = yxyLessonsSynchroLesson.getDuration();
                // model.name = yxyLessonsSynchroLesson.getName();
                // model.author = yxyLessonsSynchroLesson.getAuthor();
                // model.vv = yxyLessonsSynchroLesson.getVv();
                // model.lessonId = yxyLessonsSynchroLesson.getId();
                //
                // // 学科、版本、分类
                // YxyLessonsSynchroCatalog catalogs =
                // yxyLessonsSynchroCatalogRepository
                // .findById(yxyLessonsSynchroLesson.getCatalogId());
                // YxyResSubject yxyResSubjects = yxyResSubjectRepository
                // .findById(catalogs.getSubjectId());
                // YxyResVersion yxyResVersions = yxyResVersionRepository
                // .findById(catalogs.getVersionId());
                // YxyLessonsSynchroGrade yxyLessonsSynchroGrades =
                // yxyLessonsSynchroGradeRepository
                // .findById(catalogs.getGradeId());
                //
                // // lessonItemId和userId
                // List<YxyLessonsSynchroLessonItem>
                // yxyLessonsSynchroLessonItemLst =
                // yxyLessonsSynchroLessonItemRepository
                // .findByLessonId(lessonId);
                // if (yxyLessonsSynchroLessonItemLst != null) {
                // for (YxyLessonsSynchroLessonItem yxyLessonsSynchroLessonItem
                // : yxyLessonsSynchroLessonItemLst) {
                // model.lessonItem = yxyLessonsSynchroLessonItem.getId();
                // String videoId = yxyLessonsSynchroLessonItem
                // .getVideoId();
                // if (videoId != null) {
                // YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                // .findById(videoId);
                // if (yxyLessonsVideos != null) {
                // model.userId = yxyLessonsVideos.getUserId();
                // }
                //
                // }
                //
                // }
                // }
                //
                // model.subjectName = yxyResSubjects.getName();
                // model.versionName = yxyResVersions.getName();
                // model.gradeName = yxyLessonsSynchroGrades.getName();
                // retuLst.add(model);
                // }
                // }
                //
                // // 同分类下的2个课程
                // else {

        // 查目录
        YxyLessonsSynchroCatalog yxyLessonsSynchroCatalogs = yxyLessonsSynchroCatalogRepository
                .findById(yxyLessonsSynchroLessons.getCatalogId());

        // 查分类
        String gradeId = yxyLessonsSynchroCatalogs.getGradeId();

        List<YxyLessonsSynchroCatalog> yxyLessonsSynchroCatalogs1 = yxyLessonsSynchroCatalogRepository
                .findByGradeId(gradeId);

        List<String> catalogIdLst = new ArrayList<>();
        for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : yxyLessonsSynchroCatalogs1) {
            catalogIdLst.add(yxyLessonsSynchroCatalog.getId());
        }

        List<YxyLessonsSynchroLesson> yxyLessonsSynchroLessonLst = yxyLessonsSynchroLessonRepository
                .findTop2ByCatalogIdInAndStatusAndIsVisibleOrderByVvDesc(
                        catalogIdLst, 2, 1);
        if (yxyLessonsSynchroLessonLst.size() >= 2) {

            for (YxyLessonsSynchroLesson yxyLessonsSynchroLesson : yxyLessonsSynchroLessonLst) {
                RespModel model = new RespModel();
                model.vip = yxyLessonsSynchroLesson.getVip();
                model.cover = yxyLessonsSynchroLesson.getCover();
                model.count = yxyLessonsSynchroLesson.getCount();
                model.duration = yxyLessonsSynchroLesson.getDuration();
                model.name = yxyLessonsSynchroLesson.getName();
                model.author = yxyLessonsSynchroLesson.getAuthor();
                model.vv = yxyLessonsSynchroLesson.getVv();
                model.lessonId = yxyLessonsSynchroLesson.getId();

                // 学科、版本、分类
                YxyLessonsSynchroCatalog catalogs = yxyLessonsSynchroCatalogRepository
                        .findById(yxyLessonsSynchroLesson.getCatalogId());
                YxyResSubject yxyResSubjects = yxyResSubjectRepository
                        .findById(catalogs.getSubjectId());
                YxyResVersion yxyResVersions = yxyResVersionRepository
                        .findById(catalogs.getVersionId());
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(catalogs.getGradeId());

                // lessonItemId和userId
                List<YxyLessonsSynchroLessonItem> yxyLessonsSynchroLessonItemLst = yxyLessonsSynchroLessonItemRepository
                        .findByLessonId(lessonId);
                for (YxyLessonsSynchroLessonItem yxyLessonsSynchroLessonItem : yxyLessonsSynchroLessonItemLst) {
                    model.lessonItem = yxyLessonsSynchroLessonItem.getId();
                    YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                            .findById(yxyLessonsSynchroLessonItem.getVideoId());
                    if (null != yxyLessonsVideos) {
                        model.userId = yxyLessonsVideos.getUserId();
                    }

                }

                model.subjectName = yxyResSubjects.getName();
                model.versionName = yxyResVersions.getName();
                model.gradeName = yxyLessonsSynchroGrades.getName();

                retuLst.add(model);
            }

        }
        // }

        return retuLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // ##
        // 学年
        public String gradeName;
        // 学科
        public String subjectName;
        // 版本
        public String versionName;

        // ##
        // vip
        public Integer vip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 贡献者
        public String author;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;
        // 课时Id
        public String lessonItem;
        // 上传者ID
        public String userId;

    }

}
